package com.fortune.service.ananlysis;


import com.faneui.common.exception.BusinessException;
import com.fortune.service.ananlysis.dto.BillMailInfoDTO;
import com.fortune.utils.AnanlysisUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service("spdbCreditCardBillMailAnanlysis")
public class SpdbCreditCardBillMailAnanlysis extends AbstractAnanlysis{

    @Override
    void ananlysis(BillMailInfoDTO billMailInfoDTO, Document document) throws BusinessException {

        Elements es = document.select("td");
        for (Element e : es) {
            String str = e.text();
            if (StringUtils.isNotBlank(str)) {
                str = str.trim().replace(" ", "");
                if (str.contains("尊敬的") && str.contains("先生：")) {
                    String custName = str.substring(str.indexOf("尊敬的") + 3, str.indexOf("先生："));
                    billMailInfoDTO.setCustName(custName);
                } else if (str.contains("到期还款日")) {
                    str = str.replace(" ","");
                    String date = str.substring(str.length() - 10);
                    AnanlysisUtils.setDate(billMailInfoDTO, date);
                } else if (str.contains("￥")) {
                    String newMoney = str.replace("￥","").replace(",","").trim();
                    BigDecimal debtMoney = new BigDecimal(newMoney);

                    AnanlysisUtils.setDebtMoney(billMailInfoDTO,debtMoney);
                }
            }
        }
    }
}
